package com.yun.baymax.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yun.baymax.R;
import com.yun.baymax.activity.ItemActivity;
import com.yun.baymax.activity.ProfileActivity;
import com.yun.baymax.bean.adapter.Trends;
import com.yun.baymax.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/27.
 */

public class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.MyViewHolder> {
    private final LayoutInflater inflater;
    Context context;
    List<Trends> datas = new ArrayList<>();

    public DiscoverAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setDatas(List<Trends> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_discover, parent, false);
        final MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myViewHolder.getAdapterPosition();
                Trends trend = datas.get(position);
                if (trend == null)
                    return;
                Intent intent = new Intent(context, ItemActivity.class);
                intent.putExtra("trend", trend);
                context.startActivity(intent);
            }
        });
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final Trends trends = datas.get(position);
        holder.tvTime.setText("" + trends.getCreatedAt());
        holder.tvPeople.setText(trends.author.getNick());
        holder.tvPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProfileActivity.class);
                if (trends.author == null)
                    return;
                intent.putExtra("user", trends.author);
                context.startActivity(intent);
            }
        });
        holder.ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProfileActivity.class);
                if (trends.author == null)
                    return;
                intent.putExtra("user", trends.author);
                context.startActivity(intent);
            }
        });
        if (!TextUtils.isEmpty(trends.author.getHeadImage()))
            Glide.with(context).load(trends.author.getHeadImage()).asBitmap().into(GlideUtils.setCircle(context, holder.ivAvatar));

        if (!TextUtils.isEmpty(trends.content))
            holder.tvContent.setText(trends.content);
        else
            holder.tvContent.setVisibility(View.GONE);
        if (TextUtils.isEmpty(trends.title))
            holder.tvTitle.setVisibility(View.GONE);
        else
            holder.tvTitle.setText(trends.title);

        LinearLayoutManager gridManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        holder.recyclerView.setLayoutManager(gridManager);
        TrendsDetailAdapter adapter = new TrendsDetailAdapter(context, trends.pics, 1);
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Trends trend = datas.get(position);
                if (trend == null)
                    return;
                Intent intent = new Intent(context, ItemActivity.class);
                intent.putExtra("trend", trend);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 10 : datas.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvPeople;
        private TextView tvTime;
        private ImageView ivImg;
        private TextView tvContent;
        private TextView tvTitle;
        private int position;
        private RecyclerView recyclerView;


        public MyViewHolder(View itemView) {
            super(itemView);
            tvPeople = (TextView) itemView.findViewById(R.id.tv_people);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            ivImg = (ImageView) itemView.findViewById(R.id.iv_img);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view);
        }

    }
}
